﻿using MvvmCross.Core.ViewModels;
using Sidvall.IdentityManager.UI.Messaging;
using System.Windows.Input;

namespace Sidvall.IdentityManager.UI.ViewModels
{
	public class NavigationViewModel : Sidvall.UI.ViewModels.ViewModelBase
	{
		#region Public Members

		#region CanShowDashboard

		private bool _CanShowDashboard;
		public bool CanShowDashboard
		{
			get
			{
				return _CanShowDashboard;
			}
			set
			{
				_CanShowDashboard = value;
				RaisePropertyChanged("CanShowDashboard");
			}
		}

		#endregion
		#region CanShowUsers

		private bool _CanShowUsers;
		public bool CanShowUsers
		{
			get
			{
				return _CanShowUsers;
			}
			set
			{
				_CanShowUsers = value;
				RaisePropertyChanged("CanShowUsers");
			}
		}

		#endregion
		#region CanShowUserProfile

		private bool _CanShowUserProfile;
		public bool CanShowUserProfile
		{
			get
			{
				return _CanShowUserProfile;
			}
			set
			{
				_CanShowUserProfile = value;
				RaisePropertyChanged("CanShowUserProfile");
			}
		}

		#endregion

		#region ShowDashboardCommand

		public ICommand ShowDashboardCommand
		{
			get
			{
				return new MvxCommand(() => ShowViewModel<DashboardViewModel>());
			}
		}

		#endregion
		#region ShowUsersCommand

		public ICommand ShowUsersCommand
		{
			get
			{
				return new MvxCommand(() => ShowViewModel<AspNetUsersCollectionViewModel>());
			}
		}

		#endregion
		#region ShowUserProfileCommand

		public ICommand ShowUserProfileCommand
		{
			get
			{
				return new MvxCommand(() => ShowViewModel<UserProfileViewModel>());
			}
		}

		#endregion

		#region Start

		public override void Start()
		{
			Subscribe<ServerManagerMessage>(OnServerManagerMessage);
			InitializeCommands();
		}

		#endregion

		#endregion
		#region Private  Members

		#region InitializeCommands

		private void InitializeCommands()
		{
			bool isEnabled;

			if (Sidvall.IdentityManager.UI.SystemContext.Current.ActiveServerItem != null)
				isEnabled = Sidvall.IdentityManager.UI.SystemContext.Current.ActiveServerItem.IsLoadedServer;
			else
				isEnabled = false;
			this.CanShowDashboard = isEnabled;
			this.CanShowUsers = isEnabled;
			this.CanShowUserProfile = isEnabled;
		}

		#endregion

		#region OnServerManagerMessage

		private void OnServerManagerMessage(ServerManagerMessage item)
		{
			InitializeCommands();
		}

		#endregion

		#endregion
	}
}
